<?php
!defined('IN_ROOT') && exit( 'Access Denied' );
class SystemController	extends	AdminModuleController
{
	public function actionHome() {
		// 订单概况
		$orderCount['pending'] = $this->OrderModel->count("orderStatus='".GODHOUSE_STATUS_ORDER_PENDING."'");
		$orderCount['confirmed'] = $this->OrderModel->count("orderStatus='".GODHOUSE_STATUS_ORDER_CONFIRMED."'");
		$orderCount['complete'] = $this->OrderModel->count("orderStatus='".GODHOUSE_STATUS_ORDER_COMPLETE."'");
		
		// 用户概况
		$userCount['unverified'] = $this->UserModel->count("status='".GODHOUSE_STATUS_UNVERIFIED."'");
		$userCount['verified']	= $this->UserModel->count("status='".GODHOUSE_STATUS_VERIFIED."'");
		
		// 产品概况
		$productCount['published'] = $this->ProductModel->count();
		
		// 文章概况
		$articleCount['published'] = $this->ArticleModel->count();
		
		$this->view->assign('orderCount', $orderCount);
		$this->view->assign('userCount', $userCount);
		$this->view->assign('productCount', $productCount);
		$this->view->assign('articleCount', $articleCount);
		$this->display('system_home');
	}
	
	// 基本设置
	public function actionSetting() {
		if(getgpc(2)) {
			unlock('config');
			$this->message('清理成功，请再次修改提交。', 'system-setting.htm');
		}
		
		if(isset($_POST['submitTo'])) {
			if(islock('config')) {
				$this->message('文件被锁定，请勿操作，如长时间被锁定请点击“ <a href="system-setting-unlock.htm">清除锁</a> ”。', 'system-setting.htm');
			}
			$config = $_POST['config'];
			
			$str = "<?php \r\n";
			foreach($config as $k=>$v) {
				$str.= "define('$k', '$v');\r\n";
			}
			
			file_put_contents(GODHOUSE_ROOT.'library/config.php', $str);
			unlock('config');
			$this->message('操作成功。', 'system-setting.htm');
		}
		
		$groupList = $this->UserGroupModel->findAll("groupId>='".GODHOUSE_GROUP_ADMIN_LIMIT."'");
		
		$this->view->assign('groupList', $groupList);
		$this->display('system_setting');
	}
	
	public function actionCache() {
		
		if(isset($_POST['submitTo'])) {
			$cacheList = getgpc('cacheList', 'P');
			foreach($cacheList as $cache) {
				$this->CacheModel->flush($cache);
			}
		}
		$this->display('system_cache');
	}
}